//格式化时间
//dateTime：时间戳
function getFormtTime(dateTime,flag) {
    if(dateTime != null ) {
        //若传入的dateTime为字符串类型，需要进行转换成数值，若不是无需下面注释代码
        var time = parseInt(dateTime)
        var date = new Date(time);
        //获取年份
        var YY = date.getFullYear();
        //获取月份
        var MM = (date.getMonth() + 1 < 10 ? '0'+(date.getMonth() + 1) : date.getMonth() + 1);
        //获取日期
        var DD = (date.getDate() < 10 ? '0'+date.getDate() : date.getDate());
        if(flag) { //flag为true，显示时分秒格式
            //获取小时
            var hh = (date.getHours() < 10 ? '0'+date.getHours() : date.getHours());
            //获取分
            var mm = (date.getMinutes() < 10 ? '0'+date.getMinutes() : date.getMinutes());
            ///获取秒
            var ss = (date.getSeconds() < 10 ? '0'+date.getSeconds() : date.getSeconds());
            //返回时间格式： 2020-11-09 13:14:52
            return YY + '-' + MM + '-' + DD + ' ' + hh + ':' + mm + ':' + ss;
        } else {
            //返回时间格式： 2020-11-09
            return YY + '-' + MM + '-' + DD;
        }
    } else {
        return "";
    }
}



$(document).ready(function (){

    queryByCondition();

});

/**
 * 查询单个数据通过多个条件模糊查询
 */
function queryType(){

    $.ajax({
        url:"/product/queryType",
        type:"post",
        data:{},
        dataType:"json",
        success:function (result){
            var str = "<option selected='selected' value='0'>请选择</option>";
            for (var i = 0; i < result.length; i++) {
                str += "<option value='" + result[i].brandId + "'>"+result[i].typeName+"</option>";
            }
            $("#ptype").html(str);
        }
    })
}
let id;
function queryBrand(){

    var bid = $("#ptype option:selected").val();
    $.ajax({
        url:"/product/queryBrand",
        type:"post",
        data:{bid:bid},
        dataType:"json",
        success:function (result){
            var str = "<option selected='selected' value='0'>请选择</option>";
            for (var i = 0; i < result.length; i++) {
                str += "<option>"+result[i].brandName+"</option>";
            }
            $("#pbrand").html(str);
        }
    })
}
function queryByCondition(){
    id = window.sessionStorage.getItem('id');
    var brand = window.sessionStorage.getItem('brand');
    var type = window.sessionStorage.getItem('type');
    var mode = window.sessionStorage.getItem('mode');
    var creatTime = window.sessionStorage.getItem('creatTime');
    var creatPeople = window.sessionStorage.getItem('creatPeople');
    var ck = window.sessionStorage.getItem('ck');
    var statu = window.sessionStorage.getItem('statu');
    var unit = window.sessionStorage.getItem('unit');
    var str = "";
    if (statu=="可用"){
        str +="    <li>\n" +
            "      <label>品牌名称</label>\n" +
            "      <select class=\"dfselect\" id='pbrand'>\n" +
            "      \t  <option>"+brand+"</option>\n" +
            "        </select>\n" +
            "      <i>必选</i>\n" +
            "    </li>\n" +
            "    <li>\n" +
            "      <label>商品类型</label>\n" +
            "      <select class=\"dfselect\" id='ptype' onchange='queryBrand()'>\n" +
            "      \t  <option>"+type+"</option>\n" +
            "        </select>\n" +
            "      <i>必选</i>\n" +
            "    </li>\n" +
            "    <li>\n" +
            "      <label>商品型号</label>\n" +
            "      <input name=\"\" type=\"text\" value='"+mode+"' class=\"dfinput\" id='pmodel' />\n" +
            "      <i>必填，不能超过50个字符</i>\n" +
            "    </li>\n" +
            "    <li>\n" +
            "      <label>单位</label>\n" +
            "      <input name=\"\" type=\"text\" value='"+unit+"' class=\"dfinput\" id='punit' />\n" +
            "      <i>必填，不能超过10个字符</i>\n" +
            "    </li>\n" +
            "    <li>\n" +
            "      <label>状态</label>\n" +
            "      <select class=\"dfselect\" id='pstatus'>\n" +
            "          <option value=\"1\" selected>可用</option>\n" +
            "          <option value=\"0\" >不可用</option>\n" +
            "        </select>\n" +
            "      <i>必选</i>\n" +
            "    </li>\n" +
            "    <li>\n" +
            "      <label>创建人</label>\n" +
            "      <input name=\"\" type=\"text\" value='"+creatPeople+"' readonly=\"readonly\" class=\"roinput\" />\n" +
            "      <i>不能编辑</i>\n" +
            "    </li>\n" +
            "    <li>\n" +
            "      <label>创建时间</label>\n" +
            "      <input name=\"\" type=\"text\" value='"+creatTime+"' readonly=\"readonly\" class=\"roinput\" />\n" +
            "      <i>不能编辑</i>\n" +
            "    </li>\n" +
            "    <li>\n" +
            "      <label>&nbsp;</label>\n" +
            "      <input name=\"\" type=\"button\" class=\"btn\" value=\"确定\" onclick='updateProduct()'/>\n" +
            "      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\n" +
            "      <input name=\"\" type=\"button\" class=\"btn\" value=\"返回\" onclick=\"window.location.href='productList.html'\"/>\n" +
            "    </li>";
        $(".forminfo").html(str);
        queryType();
    }
    else if (statu=="不可用"){
        str +="    <li>\n" +
            "      <label>品牌名称</label>\n" +
            "      <select class=\"dfselect\" id='pbrand'>\n" +
            "      \t  <option>"+brand+"</option>\n" +
            "        </select>\n" +
            "      <i>必选</i>\n" +
            "    </li>\n" +
            "    <li>\n" +
            "      <label>商品类型</label>\n" +
            "      <select class=\"dfselect\" id='ptype' onchange='queryBrand()'>\n" +
            "      \t  <option>"+type+"</option>\n" +
            "        </select>\n" +
            "      <i>必选</i>\n" +
            "    </li>\n" +
            "    <li>\n" +
            "      <label>商品型号</label>\n" +
            "      <input name=\"\" type=\"text\" value='"+mode+"' class=\"dfinput\" id='pmodel' />\n" +
            "      <i>必填，不能超过50个字符</i>\n" +
            "    </li>\n" +
            "    <li>\n" +
            "      <label>单位</label>\n" +
            "      <input name=\"\" type=\"text\" value='"+unit+"' class=\"dfinput\" id='punit' />\n" +
            "      <i>必填，不能超过10个字符</i>\n" +
            "    </li>\n" +
            "    <li>\n" +
            "      <label>状态</label>\n" +
            "      <select class=\"dfselect\" id='pstatus'>\n" +
            "          <option value=\"1\" >可用</option>\n" +
            "          <option value=\"0\" selected>不可用</option>\n" +
            "        </select>\n" +
            "      <i>必选</i>\n" +
            "    </li>\n" +
            "    <li>\n" +
            "      <label>创建人</label>\n" +
            "      <input name=\"\" type=\"text\" value='"+creatPeople+"' readonly=\"readonly\" class=\"roinput\" />\n" +
            "      <i>不能编辑</i>\n" +
            "    </li>\n" +
            "    <li>\n" +
            "      <label>创建时间</label>\n" +
            "      <input name=\"\" type=\"text\" value='"+creatTime+"' readonly=\"readonly\" class=\"roinput\" />\n" +
            "      <i>不能编辑</i>\n" +
            "    </li>\n" +
            "    <li>\n" +
            "      <label>&nbsp;</label>\n" +
            "      <input name=\"\" type=\"button\" class=\"btn\" value=\"确定\" onclick='updateProduct()'/>\n" +
            "      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\n" +
            "      <input name=\"\" type=\"button\" class=\"btn\" value=\"返回\" onclick=\"window.location.href='productList.html'\"/>\n" +
            "    </li>";
        $(".forminfo").html(str);
        queryType();
    }
}



function updateProduct(){

    var pBrand = $("#pbrand option:selected").text();
    var pType = $("#ptype option:selected").text();
    var pTypeId = $("#ptype option:selected").val();
    var pModel = $("#pmodel").val();
    var pUnit = $("#punit").val();
    var pStatus = $("#pstatus option:selected").val();
    var pId =id;
    $.ajax({
        url:"/product/updateProduct",
        type:"post",
        data:{pId:pId,pBrand:pBrand,pType:pType,pModel:pModel,pUnit:pUnit,pStatus:pStatus,pTypeId:pTypeId},
        dataType:"json",
        success:function (result){
            if (result=="true"){
                alert("修改成功");
                window.location="productList.html";
            }else if (result=="false"){
                alert("修改失败");
            }else if (result=="falied"){
                alert("修改的商品型号不存在，请重新修改");
            }
        }
    })
}